package com.ultrapower.util.topology;

import com.ultrapower.bean.ApplicationBean;

public class LinkKey {
	private final String fromApplication;
	private final short fromServiceTypeCode;

	private final String toApplication;
	private final short toServiceTypeCode;

	private int hash;

	public LinkKey(ApplicationBean fromApplication, ApplicationBean toApplication) {
		if (fromApplication == null) {
			throw new NullPointerException("fromApplication must not be null");
		}
		if (toApplication == null) {
			throw new NullPointerException("toApplication must not be null");
		}
		this.fromApplication = fromApplication.getName();
		this.fromServiceTypeCode = fromApplication.getServiceTypeCode();

		this.toApplication = toApplication.getName();
		this.toServiceTypeCode = toApplication.getServiceTypeCode();
	}

	public LinkKey(String fromApplication, short fromServiceType, String toApplication, short toServiceType) {
		if (fromApplication == null) {
			throw new NullPointerException("fromApplication must not be null");
		}
		if (toApplication == null) {
			throw new NullPointerException("toApplication must not be null");
		}

		this.fromApplication = fromApplication;
		this.fromServiceTypeCode = fromServiceType;

		this.toApplication = toApplication;
		this.toServiceTypeCode = toServiceType;
	}

	public String getFromApplication() {
		return fromApplication;
	}

	public short getFromServiceType() {
		return fromServiceTypeCode;
	}

	public String getToApplication() {
		return toApplication;
	}

	public short getToServiceType() {
		return toServiceTypeCode;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((fromApplication == null) ? 0 : fromApplication.hashCode());
		result = prime * result + fromServiceTypeCode;
		result = prime * result + ((toApplication == null) ? 0 : toApplication.hashCode());
		result = prime * result + toServiceTypeCode;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LinkKey other = (LinkKey) obj;
		if (fromApplication == null) {
			if (other.fromApplication != null)
				return false;
		} else if (!fromApplication.equals(other.fromApplication))
			return false;
		if (fromServiceTypeCode != other.fromServiceTypeCode)
			return false;
		if (toApplication == null) {
			if (other.toApplication != null)
				return false;
		} else if (!toApplication.equals(other.toApplication))
			return false;
		if (toServiceTypeCode != other.toServiceTypeCode)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "LinkKey [fromApplication=" + fromApplication + ", fromServiceTypeCode=" + fromServiceTypeCode
				+ ", toApplication=" + toApplication + ", toServiceTypeCode=" + toServiceTypeCode + ", hash=" + hash
				+ "]";
	}

}
